package com.example.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.SysQuestionAnswer;
import com.example.demo.mapper.SysQuestionAnswerMapper;
import com.example.demo.service.SysQuestionAnswerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ld&zjw
 * @since 2024-01-27
 */
@Service
public class SysQuestionAnswerServiceImpl extends ServiceImpl<SysQuestionAnswerMapper, SysQuestionAnswer> implements SysQuestionAnswerService {

    @Resource
    SysQuestionAnswerMapper newsMapper;

    @Override
    public boolean save(SysQuestionAnswer news) {
        news.setCreateTime(new Date());
        int insert = newsMapper.insert(news);
        if (insert == 1)
            return true;
        else
            return false;
    }

    @Override
    public int update(SysQuestionAnswer news) {
        return newsMapper.updateById(news);
    }

    @Override
    public int update(Long id) {
        return newsMapper.deleteById(id);
    }

    @Override
    public SysQuestionAnswer getById(Long id) {
        return newsMapper.selectById(id);
    }

    @Override
    public Page<SysQuestionAnswer> findPage(Integer pageNum, Integer pageSize, String search) {
        LambdaQueryWrapper<SysQuestionAnswer> wrapper = Wrappers.<SysQuestionAnswer>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(SysQuestionAnswer::getTitle, search);
        }
        Page<SysQuestionAnswer> newsPage = newsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return newsPage;
    }

}
